package com.dgut.app.controller;

import com.dgut.app.service.UserService;
import com.dgut.common.dto.WxUserRegisterDTO;
import com.dgut.common.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author IT00ZYQ
 * @Date 2021/4/4 10:59
 **/
@RequestMapping("/app/user")
@RestController
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 微信用户注册
     * @param dto 注册信息
     * @return token
     */
    @PostMapping("/register")
    public R register(@RequestBody @Valid WxUserRegisterDTO dto) {
        return R.ok().data("token", userService.register(dto));
    }

    /**
     * 微信用户登录
     * @param code 微信临时票据
     * @return token
     */
    @PostMapping("/login/{code}")
    public R login(@PathVariable("code") String code) {
        return R.ok().data("token", userService.login(code));
    }

    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    public R info(@RequestHeader("token") String token){
        return R.ok().data("userInfo", userService.userInfo(token));
    }


}
